﻿using MugenInjection.Scope;
using MugenInjection.Web.Interface;
using MugenInjection.Web.Scope;

namespace MugenInjection.Web.Config
{
    /// <summary>
    /// Providing integration with http context. Added support for HttpRequestScope and SessionScope.
    /// </summary>
    public class MugenInjectionHttpApplicationConfig : IConfiguration
    {
        #region Implementation of IConfiguration

        /// <summary>
        /// Loads the current configuration.
        /// </summary>
        /// <param name="configuration">The specified <see cref="IApplicationConfiguration"/>.</param>
        public void Load(IApplicationConfiguration configuration)
        {
            configuration.Injector.Bind<IScopeProvider>()
                .ToConstant(new SessionScopeProvider(configuration))
                .NamedBinding(WebContextScopeLifecycle.SessionScopeProvider);

            configuration.Injector.Bind<IScopeProvider>()
                .ToConstant(new HttpRequestScopeProvider(configuration))
                .NamedBinding(WebContextScopeLifecycle.HttpRequestScopeProvider);
        }

        /// <summary>
        /// Unloads the current configuration.
        /// </summary>
        /// <param name="configuration">The specified <see cref="IApplicationConfiguration"/>.</param>
        public void Unload(IApplicationConfiguration configuration)
        {
            configuration.Injector
                .Unbind<IScopeProvider>(WebContextScopeLifecycle.SessionScopeProvider);

            configuration.Injector
                .Unbind<IScopeProvider>(WebContextScopeLifecycle.HttpRequestScopeProvider);
        }

        #endregion
    }
}